<?php
/**
 * 权限管理相关操作
 * 2016-9-9 10:41:39
 * @version 1.0
 */
namespace Home\Controller;
class AuthmanageController extends PmpController
{
    /** 
     * 首页
     * 2016-9-9 10:41:47
     * @version 1.0
     */
    public function index()
    {
        $groupInfoModel = D("GroupInfo");
        $groupArray = $groupInfoModel->getGroupInfo();
        foreach ($groupArray as $key => $value) 
        {
        	if($value['name'] == "超级管理员")
        	{
        		unset($groupArray[$key]);
        	}
        }
        $this->assign("groupArray", $groupArray);
        $this->display();
    }
    
    /**
     * 添加分组
     * 2016-9-9 10:41:47
     * @version 1.0
     */
    public function add()
    {
        $this->display();
    }
    
    /**
     * 添加
     * 2016-9-20 10:06:15
     * sz
     */
    public function submit_add ()
    {
        $group_name = $_POST['group_name'];
        $group_introduce = $_POST['group_introduce'];
        $groupInfoModel = D("GroupInfo");
        
        // 判断分组是否已经存在
        $selFlag = $groupInfoModel->getGroupByName($group_name);
        if($selFlag != null)
        {
        	echo "-9";
        }
        else 
        {
            // 判断是否添加成功
            $addFlag = $groupInfoModel->addGroup($group_name, $group_introduce);
            if ($addFlag >= 0)
            {
                $logArray[$this->operationAdd][] = "【权限管理】->【添加分组】";
                $this->addLog($logArray);
                echo "1";
            }
            else 
            {
            	echo "0";
            }
        }
    }
    
    /**
     * 设置权限
     * 2016-9-9 10:41:47
     * @version 1.0
     */
    public function set()
    {
        $group_id = $_REQUEST['group_id'];
        $authArray = D("AuthInfo")->getGroupInfo();
        
        // 根据group_id获取分组详情
        $groupArray = D("GroupInfo")->getGroupById ($group_id);
        
        // 遍历数组，获取一级目录
        $totalArray = array();
        $listArray = array();
        foreach ($authArray as $key => $value) 
        {
            $tag = explode(',', $groupArray[0]['auth_ids']);
            if(in_array($value['id'], $tag))
            {
                $value['auth_flag'] = "checked";
            }
            else 
            {
                $value['auth_flag'] = "";
            }
            if($value['parent_id'] == '0')
            {
                $totalArray[$value['id']] = $value;
            }
            else 
            {
                $totalArray[$value['parent_id']]['detail'][] = $value;
            }
        }
        
        $authHtml = "";
        $tagNo = 1;
        foreach ($totalArray as $key => $value) 
        {
            $authHtml .= "<tr>";
            $authHtml .= "<td><div class='checkbox checkbox-primary checkbox-single'><input id='checkbox";
            $authHtml .= $tagNo;
            $authHtml .= "' type='checkbox' class='controller' ";
            $authHtml .= $value['auth_flag'];
            $authHtml .= "><label for='checkbox";
            $authHtml .= $tagNo;
            $authHtml .= "'>";
            $authHtml .= $value['name'];
            $authHtml .= "</label></div></td>";
            $authHtml .= "<td>";
            $minTagNo = 0;
            foreach ($value['detail'] as $key1 => $value1) 
            {
                $authHtml .= "<div class='checkbox checkbox-primary checkbox-single'><input id='checkbox";
                $authHtml .= ($tagNo . $minTagNo);
                $authHtml .= "' type='checkbox' name='rules[]' value='";
                $authHtml .= $value1['id'];
                $authHtml .= "' class='action' ";
                $authHtml .= $value1['auth_flag'];
                $authHtml .= "><label for='checkbox";
                $authHtml .= ($tagNo . $minTagNo);
                $authHtml .= "'>";
                $authHtml .= $value1['name'];
                $authHtml .= "</label></div>";
                $minTagNo++;
            }
            $authHtml .= "</td></tr>";
            $tagNo++;
        }
        
        $this->assign("group_id", $group_id);
        $this->assign("authHtml", $authHtml);
        $this->display();
    }
    
    /**
     * 添加
     * 2016-9-20 10:06:15
     * sz
     */
    public function submit_set ()
    {
        $group_id = $_POST['group_id'];
        $rules = $_POST['rules'];
        $auth_ids = implode(",", $rules);
        $groupInfoModel = D("GroupInfo");
   
        // 判断是否添加成功
        $addFlag = $groupInfoModel->setAuth($group_id, $auth_ids);
        if ($addFlag >= 0)
        {
            $logArray[$this->operationModify][] = "【权限管理】->【设置权限】";
            $this->addLog($logArray);
            header("Location: ?c=authmanage&a=index");
        }
    }
    
    /**
     * 修改分组
     * 2016-9-14 10:45:33  
     */
    public function modify()
    {
        $group_id = $_REQUEST['id'];
        $groupInfoModel = D("GroupInfo");
        $groupArray = $groupInfoModel->getGroupById ($group_id);
        $this->assign("groupInfo", $groupArray[0]);
        $this->display();
    }
    
    /**
     * 添加
     * 2016-9-20 10:06:15
     * sz
     */
    public function submit_modify ()
    {
        $group_id = $_POST['group_id'];
        $old_group_name = $_POST['old_group_name'];
        $group_name = $_POST['group_name'];
        $group_introduce = $_POST['group_introduce'];
        $groupInfoModel = D("GroupInfo");
    
        // 判断分组是否已经存在
        $selFlag = $groupInfoModel->getGroupByName($group_name);
        if($selFlag != null && $old_group_name != $group_name)
        {
            echo "-9";
        }
        else
        {
            // 判断是否添加成功
            $addFlag = $groupInfoModel->modifyGroup($group_id, $group_name, $group_introduce);
            if ($addFlag >= 0)
            {
                $logArray[$this->operationAdd][] = "【权限管理】->【修改分组】";
                $this->addLog($logArray);
                echo "1";
            }
            else
            {
                echo "0";
            }
        }
    }
    
    /**
     * 删除
     * 2016-9-14 09:17:25
     * sz
     */
    public function del ()
    {
        $id = $_POST['id'];
        $groupInfoModel = D("GroupInfo");
        // 判断是否删除成功
        $delFlag = $groupInfoModel->del($id);
        if ($delFlag >= 0)
        {
            $logArray[$this->operationDelete][] = "【权限管理】->【删除分组】";
            $this->addLog($logArray);
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
}